#include    "blinds.h"

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Blinds::Blinds(QObject *parent) : Device(parent)
  , motion_time(1.0)
  , state(false)
  , is_opened(false)
  , max_pos(1.0)
{

}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
Blinds::~Blinds()
{

}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Blinds::setState(bool state)
{
    this->state = state;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
bool Blinds::isOpened() const
{
    return is_opened;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
float Blinds::getPosition() const
{
    return static_cast<float>(getY(0));
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Blinds::preStep(state_vector_t &Y, double t)
{
    Q_UNUSED(t)

    is_opened = static_cast<bool>(hs_p(Y[0] - 0.95 * max_pos));
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Blinds::ode_system(const state_vector_t &Y,
                        state_vector_t &dYdt,
                        double t)
{
    Q_UNUSED(t)

    dYdt[0] = 3.0 * (max_pos * static_cast<double>(state) - Y[0]) / motion_time;
}

//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void Blinds::load_config(CfgReader &cfg)
{
    QString secName = "Device";

    cfg.getDouble(secName, "MotionTime", motion_time);
    cfg.getDouble(secName, "MaxPos", max_pos);
}
